package com.rfs.suanfa;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author renfushuai
 * @date 2022/7/25
 */
public class 无重复字符的最长子串 {
    public static void main(String[] args) {
        int pwwkew = lengthOfLongestSubstring("abcabcbb");
        System.out.println(pwwkew);
    }

    /**
    * @author: rfs
    * @create: 2022/7/25
    * @description: 滑动窗口
    **/
    public static int lengthOfLongestSubstring(String s) {
        int max = 0;
        int start = 0;
        int end = 0;
        Map<Character, Integer> map = new HashMap<>();
        while (end < s.length()) {
            char c = s.charAt(end);
            if (map.containsKey(c)) {
                start = Math.max(start, map.get(c) + 1);
            }
            map.put(c, end);
            max = Math.max(max, end - start + 1);
            end++;
        }
        return max;
    }
}
